﻿namespace D.Domain.SystemDomain.SysOrgDomain.Entities
{
    /// <summary>
    /// 组织部门实体
    /// </summary>
    public class SysOrgEntity : BaseEntity
    {
        /// <summary>
        /// 部门名称
        /// </summary>

        public string Name { get; private set; }

        /// <summary>
        /// 部门编码
        /// </summary>
        public string Code { get; private set; }

        /// <summary>
        /// 部门负责人
        /// </summary>
        public Guid? SysAdminId { get; private set; }

        /// <summary>
        /// 办公室位置
        /// </summary>
        public string Location { get; private set; }

        /// <summary>
        /// 上级部门ID
        /// </summary>
        public Guid? ParentId { get; private set; }

        /// <summary>
        /// 部门描述
        /// </summary>
        public string Descption { get; private set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; private set; }

        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime? UpdateTime { get; private set; }

        /// <summary>
        /// 创建人
        /// </summary>
        public string Creater { get; private set; }

        /// <summary>
        /// 更新人
        /// </summary>
        public string Updater { get; private set; }

        /// <summary>
        /// 是否主组织
        /// </summary>
        public bool IsMain { get; private set; }

        public SysOrgEntity()
        {

        }
        public SysOrgEntity(string name, string descpiton, string location)
        {
            this.Id = Guid.NewGuid();
            this.Name = name;
            this.Descption = descpiton;
            this.Location = location;
            this.IsDeleted = false;
            this.CreateTime = DateTime.Now;
            // 添加事件
            AddEventIfAbsent(new SysOrgAddedEvent(this));
        }

        public void Update(string name)
        {
            Name = name;
        }

        public void ChangeParentId(Guid? value)
        {
            this.ParentId = value;
            this.UpdateTime = DateTime.Now;
            AddEventIfAbsent(new SysOrgUpdatedEvent(this));
        }
        public void ChangeName(string value)
        {
            this.Name = value;
            this.UpdateTime = DateTime.Now;
            AddEventIfAbsent(new SysOrgUpdatedEvent(this));
        }
        public void ChangeSysAdminId(Guid? value)
        {
            this.SysAdminId = value;
            this.UpdateTime = DateTime.Now;
            AddEventIfAbsent(new SysOrgUpdatedEvent(this));
        }
        public void SoftDelete()
        {
            this.IsDeleted = true;
            this.UpdateTime = DateTime.Now;
            AddEvent(new SysOrgDeletedEvent(Id));
        }
    }
}
